package com.zhou.yuefan.application;

import java.io.File;

import com.zhou.yuefan.db.DBManager;

import android.app.Application;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class MyApplication extends Application {
	
	private String name ;
	private boolean isFirstLoad ;
	private boolean isDataBaseExist ;
	public static final String PACKAGE_NAME = "com.zhou.yuefan";
	public static final String SHARED_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/shared_prefs";
	
	private DBManager dbManager;
	
	
	
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		this.name = "zhou" ;
		setDataBaseExist(isDataBaseExists("waimai"));
		setFirstLoad(!isSharedprefsExists("firstLoad"));
		Log.i("zhou", "程序启动了");
	}
	
	/**
	 * Lazy单例模式获取dbManager实例
	 * @return
	 */
	public DBManager getDBInstance(){
		if(dbManager==null){
			dbManager = new DBManager(this);
		}
		return dbManager;
	}
	
	public String getName(){
		return this.name ;
	}
	public void setName(String name){
		this.name = name ;
	}
	
	/**
	 * 判断sharedPreferrences是否已经存在
	 * @param prefsName 文件名，不加.xml
	 * @return
	 */
	public boolean isSharedprefsExists(String prefsName){
		SharedPreferences sp = getSharedPreferences("firstLoad", MODE_PRIVATE);
		return sp.contains("LT");
		
	}
	
	/**
	 * 判断Database数据库是否存在
	 * @param dbName 数据库名
	 * @return
	 */
	public boolean isDataBaseExists(String dbName){
		String fileName = DBManager.DATABASE_FILENAME;
		Log.i("DBfileName",fileName);
		File file = new File(fileName);
		return file.exists();
	}
	
	

	public boolean isFirstLoad() {
		 SharedPreferences sp = getSharedPreferences("firstLoad", MODE_PRIVATE);
		 Log.i("isFirstLoad", !sp.contains("LT")+"");
		return !sp.contains("LT");
	}

	public void setFirstLoad(boolean isFirstLoad) {
		this.isFirstLoad = isFirstLoad;
	}

	public boolean isDataBaseExist() {
		return isDataBaseExist;
	}

	public void setDataBaseExist(boolean isDataBaseExist) {
		this.isDataBaseExist = isDataBaseExist;
	}
	
	
	
}
